package cc.rengu.igas.channel.wechat.core.service.trans;

import cc.rengu.igas.channel.wechat.common.constant.WechatParamConstant;
import cc.rengu.igas.channel.wechat.common.constant.WechatTreeNodeConstant;
import cc.rengu.igas.channel.wechat.core.model.bean.IdcInfo;
import cc.rengu.oltp.service.base.RadpService;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.utility.util.RedisUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import com.alibaba.fastjson.JSON;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 查询网联idc列表
 */
public class NuwxIdcQueryService extends RadpService {
    @Override
    protected int bizInit(String s) {
        return 0;
    }

    @Override
    protected int bizProcess(String s, int i) {
        XmlTreeUtil tree = new XmlTreeUtil();
        int num = 1;
        String idcFlag = null;
        String mchtId = XmlConfigUtil.getXmlConfigCacheKeyValue("nuccWechat", "mchId");

        rglog.info("mchtId=[{}]", mchtId);
        try {
            if (mchtId != null) {
                tree.setXmlTreeStringValue("providMchntNo", mchtId);
                tree.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, "idcquery");
            } else {
                rglog.error("获取mchId失败！");
                return -1;
            }
            /* 调用网联第三方通讯接口 */
            int iReturnCode = schedule.callService("NuccWechatClient", 0);
            if (0 != iReturnCode) {
                rglog.error("与网联通信失败，iReturnCode=[{}]", iReturnCode);
                return -1;
            }
            //更新idc信息
            String code = tree.getXmlTreeStringValue("returnCode");

            String errCodeDes = tree.getXmlTreeStringValue("returnMsg");

            if (!"SUCCESS".equals(code)) {
                rglog.error("网联第三方服务返回业务结果不成功，code={},msg={}", code, errCodeDes);
                return -1;
            }

            String idcInfos = tree.getXmlTreeStringValue("idcInfos");
            rglog.debug("idcInfos=<{}>", idcInfos);
            List<IdcInfo> idcInfoList = JSON.parseArray(idcInfos, IdcInfo.class);
            StringBuilder stringBuilder = new StringBuilder();
            if (null != idcInfoList && idcInfoList.size() > 0) {
                Iterator<IdcInfo> itContact = idcInfoList.iterator();
                while (itContact.hasNext()) {
                    IdcInfo idcInfo = itContact.next();
                    rglog.error("idcFlag=[{}],status[{}]", idcInfo.getIdcFlag(), idcInfo.getStatus());
                    if ("02".equals(idcInfo.getStatus())) {
                        continue;
                    }
                    stringBuilder.append(idcInfo.getIdcFlag());
                    stringBuilder.append("|");
                    num++;
                }
            } else {
                rglog.error("未获取到idc列表");
                return -1;
            }
            idcFlag = stringBuilder.substring(0, stringBuilder.length() - 1).toString();
            rglog.error("idcFlag=[{}]", idcFlag);
            Map<String, String> payParam = new HashMap<>();
            payParam.put(WechatTreeNodeConstant.nuccIdcNum, String.valueOf(num - 1));
            payParam.put(WechatTreeNodeConstant.nuccIdcList, idcFlag);
            RedisUtil.onceSet(WechatParamConstant.NUCC_IDC_CAC, JSON.toJSONString(payParam));

        } catch (Exception e) {
            rglog.error(e.getMessage());
            return -1;
        }
        return 0;
    }

    @Override
    protected int bizTerminate(String s) {
        return 0;
    }
}
